class Node{
    constructor(element){
        this.element = element;
        this.prev = null;
        this.next = null;
    }
}

class DoubleList{
    constructor(){
        this.head = null; //记录头元素/节点
        this.tail = null; //记录尾元素/节点
        this.count = 0;   //记录节点数量
    }

    //添加
    push(element){
        //创建节点
        let node = new Node(element);
        if(this.head === null){
            //1.当前链表为空
            this.head = node;
            this.tail = node;
        }else{
            let last = this.tail; //最后一个节点
            last.next = node;
            node.prev = last;
            this.tail = node;
        }
        this.count++;
    }

    //获取指定下标的节点
    getElementAt(index){ //1
        let current = this.head;
        for(let i = 0;i<index;i++){
            current = current.next;
        }
        return current
    }

    //插入
    insert(element,index){
        //1.边界值的判断
        if(index >= 0 && index <= this.count){
            let node = new Node(element);
            if(index === 0){
                //1.在开头插入
                if(this.head === null){
                    //当前链表为空
                    this.head = node;
                    this.tail = node;
                }else{
                    //当前链表不为空
                    let current = this.head;
                    node.next = current;
                    current.prev = node;
                    this.head = node;
                }
            }else if(index === this.count){
                //2.在末尾插入
                let last = this.tail;
                last.next = node;
                node.prev = last;
                this.tail = node;
            }else{
                //3.在中间插入
                let current = this.getElementAt(index);

                let prev = current.prev;

                prev.next = node;
                node.prev = prev;

                node.next = current;
                current.prev = node;
            }
            this.count++;
        }else{
            return false
        }
    }

    //删除
    removeAt(index){
        //1.边界值的判断
        if(index >= 0 && index < this.count){
            if(index === 0){
                //1.从开头删除
                if(this.count === 1){
                    //只有一个节点
                    this.head = null;
                    this.tail = null;
                }else{
                    let current = this.head;
                    this.head  = current.next;
                    current.next.prev = null;
                }
            }else if(index === (this.count-1)){
                //2.从末尾删除
                let last = this.tail;
                this.tail = last.prev;
                this.tail.next = null;

            }else{
                 //3.从中间删除

                 let current = this.getElementAt(index);

                 current.prev.next = current.next;
                 current.next.prev = current.prev;
            }
            this.count--;
            //删除的元素return
        }else{
            return undefined
        }
    }
}

let d = new DoubleList();

d.push('a');

d.push('b');

d.push('c');

// d.insert('a',0);
// d.insert('b',0);

// d.insert('c',2);

// d.insert('d',2);

//b  a  d c

d.removeAt(2);

console.log(d);